import load from '@/utils/jsapi.js';

const maplayersaddutils = {

  async initiallayers(layers, maptoken) {
    const [
      TileLayer,
      SceneLayer,
      VectorTileLayer,
      MapImageLayer,
      SHCDMapImageLayer,
      IntegratedMeshLayer,
      PointCloudLayer,
      SHCTiledMapServiceLayer,
      SHCMapServiceLayer,
      GroupLayer,
      FeatureLayer,
      GraphicsLayer,
      Extent,
      SpatialReference
    ] = await load([
      "esri/layers/TileLayer",
      "esri/layers/SceneLayer",
      "esri/layers/VectorTileLayer",
      "esri/layers/MapImageLayer",
      "esri/layers/SHCDMapImageLayer",
      "esri/layers/IntegratedMeshLayer",
      "esri/layers/PointCloudLayer",
      "esri/layers/SHCTiledMapServiceLayer",
      "esri/layers/SHCMapServiceLayer",
      "esri/layers/GroupLayer",
      "esri/layers/FeatureLayer",
      "esri/layers/GraphicsLayer",
      "esri/geometry/Extent",
      "esri/geometry/SpatialReference"
    ]);

    return layers.map(function (item, key, ary) {
      switch (item.maptype) {
        case "MapImageLayer":
          return new MapImageLayer(item.url, {
            id: item.id,
            visible: item.visible,
            opacity: item.opacity,
            listMode: item.listMode,
            title: item.title
          });
          break;
        case "FeatureLayer":
          return new FeatureLayer(item.url, {
            id: item.id,
            visible: item.visible,
            opacity: item.opacity,
            listMode: item.listMode,
            title: item.title,
            elevationInfo:item.elevationInfo||"on-the-ground",
            screenSizePerspectiveEnabled: item.screenSizePerspectiveEnabled
          });
          break;
        case "TileLayer":
          return new TileLayer(item.url, {
            id: item.id,
            visible: item.visible,
            opacity: item.opacity,
            listMode: item.listMode,
            title: item.title
          });
          break;
        case "GraphicsLayer":
          return new GraphicsLayer({
            id: item.id,
            visible: item.visible,
            opacity: item.opacity,
            listMode: item.listMode,
            title: item.title
          });
          break;
        case "PointCloudLayer":
          return new PointCloudLayer(layer.url, {
            id: layer.id,
            title: layer.title,
            visible: layer.visible,
            listMode: item.listMode,
            //  opacity: layer.opacity,
            listMode: layer.listMode
          });
          break;
        case "SceneLayer":
          return new SceneLayer(item.url, {
            id: item.id,
            title: item.title,
            visible: item.visible,
            opacity: item.opacity,
            renderer: item.renderer,
            listMode: item.listMode,
            popupEnabled: item.popupEnabled
          });
          break;
        case "SHCTiledMapServiceLayer":
          const fExtent = new Extent({
            "xmin": -65000,
            "ymin": -76000,
            "xmax": 75000.00000000003,
            "ymax": 72000.00000000003,
            "spatialReference": SpatialReference.WebMercator
          });
          if (item.istoken === true) {
            return new SHCTiledMapServiceLayer({
              url: item.url,
              token: maptoken,
              fullExtent: fExtent,
              opacity: item.opacity,
              title: item.title,
              id: item.id,
              visible: item.visible
            });
          } else {
            return new SHCTiledMapServiceLayer({
              url: item.url,
              fullExtent: fExtent,
              opacity: item.opacity,
              title: item.title,
              id: item.id,
              visible: item.visible
            });
          }
          break;
        case "SHCMapServiceLayer":
          const fuExtent = new Extent({
            "xmin": -65000,
            "ymin": -76000,
            "xmax": 75000.00000000003,
            "ymax": 72000.00000000003,
            "spatialReference": SpatialReference.WebMercator
          });
          if (item.istoken === true) {
            return new SHCMapServiceLayer({
              url: item.url,
              token: maptoken,
              fullExtent: fuExtent,
              opacity: item.opacity,
              title: item.title,
              id: item.id,
              visible: item.visible
            });
          } else {
            return new SHCMapServiceLayer({
              url: item.url,
              fullExtent: fuExtent,
              opacity: item.opacity,
              title: item.title,
              id: item.id,
              visible: item.visible
            });
          }
          break;
      }
    });
  },

  async initial_kz_layers(layers, maptoken) {
    const [
      TileLayer,
      SceneLayer,
      VectorTileLayer,
      MapImageLayer,
      SHCDMapImageLayer,
      IntegratedMeshLayer,
      PointCloudLayer,
      SHCTiledMapServiceLayer,
      SHCMapServiceLayer,
      GroupLayer,
      FeatureLayer,
      Extent,
      SpatialReference
    ] = await load([
      "esri/layers/TileLayer",
      "esri/layers/SceneLayer",
      "esri/layers/VectorTileLayer",
      "esri/layers/MapImageLayer",
      "esri/layers/SHCDMapImageLayer",
      "esri/layers/IntegratedMeshLayer",
      "esri/layers/PointCloudLayer",
      "esri/layers/SHCTiledMapServiceLayer",
      "esri/layers/SHCMapServiceLayer",
      "esri/layers/GroupLayer",
      "esri/layers/FeatureLayer",
      "esri/geometry/Extent",
      "esri/geometry/SpatialReference"
    ]);

    return layers.map(function (item, key, ary) {
      switch (item.maptype) {
        case "MapImageLayer":
          window[item.id + '_legend'] = new MapImageLayer(item.url, {
            id: item.id + '_legend',
            url: item.url,
            title: item.title,
            //  definitionExpression: '1==1',
            visible: item.visible,
            opacity: item.opacity,
            listMode: item.listMode
          })
          window.map.add(window[item.id + '_legend']);
          break;
        case "MapImageLayerdisplay":
          if (window.xzcode != null&&window.xzcode != "xuhui") {
            window[item.id] = new MapImageLayer(item.url, {
              id: item.id,
              url: item.url,
              title: item.title,
              //  definitionExpression: '1==1',
              visible: item.visible,
              opacity: item.opacity,
              listMode: item.listMode
            })
            window.map.add(window[item.id]);
          }
          break;
        case "SHCDMapImageLayer":
          if (window.xzcode == null || window.xzcode == "xuhui") {
            const fuxExtent = new Extent({
              "xmin": -65000,
              "ymin": -76000,
              "xmax": 75000.00000000003,
              "ymax": 72000.00000000003,
              "spatialReference": SpatialReference.WebMercator
            });
            window[item.id] = new SHCDMapImageLayer({
              url: item.url,
              fullExtent: fuxExtent,
              opacity: item.opacity,
              title: item.title,
              id: item.id,
              tileurl: item.tileurl,
              visible: item.visible,
              listMode: item.listMode,
              legendEnabled: false,
              sublayers: item.sublayers,
              dLayer: window[item.id + '_legend']
            });
            window.map.add(window[item.id]);
          }
          break;
        case "SceneLayer":
          window[item.id] = new SceneLayer(item.url, {
            id: item.id,
            title: item.title,
            visible: item.visible,
            opacity: item.opacity,
            renderer: item.renderer,
            listMode: item.listMode,
            popupEnabled: item.popupEnabled
          });
          window.map.add(window[item.id]);
          break;
      }
    })
  },

  async initalGroupAndlayers(layerGroups, maptoken) {
    const [
      TileLayer,
      SceneLayer,
      VectorTileLayer,
      MapImageLayer,
      IntegratedMeshLayer,
      PointCloudLayer,
      SHCTiledMapServiceLayer,
      SHCMapServiceLayer,
      GroupLayer,
      FeatureLayer,
      GraphicsLayer,
      Extent,
      SpatialReference
    ] = await load([
      "esri/layers/TileLayer",
      "esri/layers/SceneLayer",
      "esri/layers/VectorTileLayer",
      "esri/layers/MapImageLayer",
      "esri/layers/IntegratedMeshLayer",
      "esri/layers/PointCloudLayer",
      "esri/layers/SHCTiledMapServiceLayer",
      "esri/layers/SHCMapServiceLayer",
      "esri/layers/GroupLayer",
      "esri/layers/FeatureLayer",
      "esri/layers/GraphicsLayer",
      "esri/geometry/Extent",
      "esri/geometry/SpatialReference"
    ]);

    return layerGroups.map(function (itemsgroup, key, ary) {
      let grouplayer = new GroupLayer({
        id: itemsgroup.id,
        title: itemsgroup.title,
        visible: itemsgroup.visible,
        listMode: itemsgroup.listMode
      });
      if (itemsgroup.layerGroups != null) {
        const groupslayers = itemsgroup.layerGroups.reverse().map(function (items, key, ary) {
          let grouplayer2 = new GroupLayer({
            id: items.id,
            title: items.title,
            visible: items.visible,
            listMode: items.listMode
          });
          items.layers.reverse().map(function (layer, key, art) {
            switch (layer.maptype) {
              case "MapImageLayer":
                grouplayer2.add(new MapImageLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity
                }))
                grouplayer2.add(mapimagelayer);
                break;
              case "PointCloudLayer":
                grouplayer2.add(new PointCloudLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity,
                  listMode: layer.listMode
                }));
                break;
              case "TileLayer":
                let layervisible = false;
                if (window.xzcode == null || window.xzcode == "xuhui") {
                  if (layer.id.indexOf("xuhui") != -1) {
                    layervisible = true;
                  } else {
                    layervisible = false;
                  }
                } else {
                  if (layer.id.indexOf(window.xzcode) != -1) {
                    layervisible = true;
                  } else {
                    layervisible = false;
                  }
                }
                grouplayer2.add(new TileLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layervisible,
                  opacity: layer.opacity
                }));
                break;
              case "FeatureLayer":
                let groupfdefinition = null;
                if (layer.definitionExpression != null) {
                  if (window.xzcode == null || window.xzcode == "xuhui") {
                    groupfdefinition = null
                  } else {
                    groupfdefinition = layer.definitionExpression + "'" + window.xzcode + "'"
                  }
                } else {
                  groupfdefinition = null
                }
                grouplayer2.add(new FeatureLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity,
                  // renderer: layer.renderer,
                  popupTemplate: layer.popupTemplate,
                  definitionExpression: groupfdefinition,
                  popupEnabled: layer.popupEnabled,
                }));
                break;
              case "SceneLayer":
                let Groupsencelayerdefinition = null;
                if (layer.definitionExpression != null) {
                  if (window.xzcode == null || window.xzcode == "xuhui") {
                    Groupsencelayerdefinition = null
                  } else {
                    Groupsencelayerdefinition = layer.definitionExpression + "'" + window.xzcode + "'"
                  }
                }
                grouplayer2.add(new SceneLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity,
                  renderer: layer.renderer,
                  popupTemplate: layer.popupTemplate,
                  definitionExpression: Groupsencelayerdefinition,
                  popupEnabled: layer.popupEnabled,
                }));
                break;
              case "SHCTiledMapServiceLayer":
                const fExtent = new Extent({
                  "xmin": -65000,
                  "ymin": -76000,
                  "xmax": 75000.00000000003,
                  "ymax": 72000.00000000003,
                  "spatialReference": SpatialReference.WebMercator
                });
                if (layer.istoken === true) {
                  grouplayer2.add(SHCTiledMapServiceLayer({
                    url: layer.url,
                    token: maptoken,
                    fullExtent: fExtent,
                    opacity: layer.opacity,
                    title: layer.title,
                    id: layer.id,
                    visible: layer.visible
                  }));
                } else {
                  grouplayer2.add(SHCTiledMapServiceLayer({
                    url: layer.url,
                    fullExtent: fExtent,
                    opacity: layer.opacity,
                    title: layer.title,
                    id: layer.id,
                    visible: layer.visible
                  }));
                }
                break;
              case "SHCMapServiceLayer":
                const fuExtent = new Extent({
                  "xmin": -65000,
                  "ymin": -76000,
                  "xmax": 75000.00000000003,
                  "ymax": 72000.00000000003,
                  "spatialReference": SpatialReference.WebMercator
                });
                if (layer.istoken === true) {
                  grouplayer2.add(SHCMapServiceLayer({
                    url: layer.url,
                    token: maptoken,
                    fullExtent: fuExtent,
                    opacity: layer.opacity,
                    title: layer.title,
                    id: layer.id,
                    visible: layer.visible
                  }));
                } else {
                  grouplayer2.add(new SHCMapServiceLayer({
                    url: layer.url,
                    fullExtent: fuExtent,
                    opacity: layer.opacity,
                    title: layer.title,
                    id: layer.id,
                    visible: layer.visible
                  }));
                }
                break;
            }
          })
          return grouplayer2;
        })
        grouplayer.addMany(groupslayers)
      }
      if (itemsgroup.layers != null) {
        const layers = itemsgroup.layers.reverse().map(function (layer, key, ary) {
          switch (layer.maptype) {
            case "MapImageLayer":
              return new MapImageLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity
              })

              break;
            case "PointCloudLayer":
              return new PointCloudLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                //  opacity: layer.opacity,
                listMode: layer.listMode
              });
              break;
            case "TileLayer":
              return new TileLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity
              });
              break;
            case "FeatureLayer":
              let fdefinition = null;
              if (layer.definitionExpression != null) {
                if (window.xzcode == null || window.xzcode == "xuhui") {
                  fdefinition = null
                } else {
                  fdefinition = layer.definitionExpression + "'" + window.xzcode + "'"
                }
              } else {
                fdefinition = null
              }
              const flayer= new FeatureLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity,
                definitionExpression: fdefinition,
                popupTemplate: layer.popupTemplate,
                elevationInfo: layer.elevationInfo,
                popupEnabled: layer.popupEnabled
              });
              if (layer.renderer != null && layer.renderer != undefined)
              flayer.renderer = layer.renderer;
              return flayer;
              break;
            case "SceneLayer":
              let scenelayerdefinition = null;
              if (layer.definitionExpression != null) {
                if (window.xzcode == null || window.xzcode == "xuhui") {
                  scenelayerdefinition = null
                } else {
                  scenelayerdefinition = layer.definitionExpression + "'" + window.xzcode + "'"
                }
              }
              return new SceneLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity,
                renderer: layer.renderer,
                definitionExpression: scenelayerdefinition,
                popupTemplate: layer.popupTemplate,
                popupEnabled: layer.popupEnabled,
              });
              break;
            case "GraphicsLayer":
              return new GraphicsLayer({
                id: layer.id,
                visible: layer.visible,
                opacity: layer.opacity,
                listMode: layer.listMode,
                title: layer.title
              });
              break;
          }
        })
        grouplayer.addMany(layers);
      }
      return grouplayer
    })
  },

  async initalGroupsORlayers(layerGroups, maptoken) {
    const [
      TileLayer,
      SceneLayer,
      VectorTileLayer,
      MapImageLayer,
      IntegratedMeshLayer,
      PointCloudLayer,
      SHCTiledMapServiceLayer,
      SHCMapServiceLayer,
      GroupLayer,
      FeatureLayer,
      Extent,
      SpatialReference
    ] = await load([
      "esri/layers/TileLayer",
      "esri/layers/SceneLayer",
      "esri/layers/VectorTileLayer",
      "esri/layers/MapImageLayer",
      "esri/layers/IntegratedMeshLayer",
      "esri/layers/PointCloudLayer",
      "esri/layers/SHCTiledMapServiceLayer",
      "esri/layers/SHCMapServiceLayer",
      "esri/layers/GroupLayer",
      "esri/layers/FeatureLayer",
      "esri/geometry/Extent",
      "esri/geometry/SpatialReference"
    ]);

    return layerGroups.reverse().map(function (itemsgroup, key, ary) {
      let grouplayer = new GroupLayer({
        id: itemsgroup.id,
        title: itemsgroup.title,
        visible: itemsgroup.visible,
      });
      if (itemsgroup.layerGroups != null) {
        const groupslayers = itemsgroup.layerGroups.reverse().map(function (items, key, ary) {
          let grouplayer2 = new GroupLayer({
            id: items.id,
            title: items.title,
            visible: items.visible,
          });
          items.layers.reverse().map(function (layer, key, art) {
            switch (layer.maptype) {
              case "MapImageLayer":
                grouplayer2.add(new MapImageLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity
                }));
                break;
              case "PointCloudLayer":
                grouplayer2.add(new PointCloudLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  //  opacity: layer.opacity,
                  listMode: layer.listMode
                }));
                break;
              case "TileLayer":
                grouplayer2.add(new TileLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity
                }));
                break;
              case "FeatureLayer":
                grouplayer2.add(new FeatureLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity,
                  renderer: layer.renderer,
                  popupTemplate: layer.popupTemplate
                }));
                break;
              case "SceneLayer":
                grouplayer2.add(new SceneLayer(layer.url, {
                  id: layer.id,
                  title: layer.title,
                  visible: layer.visible,
                  opacity: layer.opacity,
                  renderer: layer.renderer
                }));
                break;
            }

          })
          return grouplayer2;
        })
        grouplayer.addMany(groupslayers)
        return grouplayer;
      } else {
        const layers = itemsgroup.layers.reverse().map(function (layer, key, ary) {
          switch (layer.maptype) {
            case "MapImageLayer":
              return new MapImageLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity
              });
              break;
            case "PointCloudLayer":
              return new PointCloudLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                //  opacity: layer.opacity,
                listMode: layer.listMode
              });
              break;
            case "TileLayer":
              return new TileLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity
              });
              break;
            case "FeatureLayer":
              return new FeatureLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity,
                renderer: layer.renderer,
                popupTemplate: layer.popupTemplate
              });
              break;
            case "SceneLayer":
              return new SceneLayer(layer.url, {
                id: layer.id,
                title: layer.title,
                visible: layer.visible,
                opacity: layer.opacity,
                renderer: layer.renderer
              });
              break;
          }
        })
        grouplayer.addMany(layers);
        return grouplayer
      }
    })
  },

  async initalLayersinGroups(layerGroups, maptoken) {
    const [
      TileLayer,
      SceneLayer,
      VectorTileLayer,
      MapImageLayer,
      IntegratedMeshLayer,
      PointCloudLayer,
      SHCTiledMapServiceLayer,
      SHCMapServiceLayer,
      GroupLayer,
      FeatureLayer,
      Extent,
      SpatialReference
    ] = await load([
      "esri/layers/TileLayer",
      "esri/layers/SceneLayer",
      "esri/layers/VectorTileLayer",
      "esri/layers/MapImageLayer",
      "esri/layers/IntegratedMeshLayer",
      "esri/layers/PointCloudLayer",
      "esri/layers/SHCTiledMapServiceLayer",
      "esri/layers/SHCMapServiceLayer",
      "esri/layers/GroupLayer",
      "esri/layers/FeatureLayer",
      "esri/geometry/Extent",
      "esri/geometry/SpatialReference"
    ]);

    return layerGroups.map(function (itemgroup, key, ary) {
      let grouplayer = new GroupLayer({
        id: itemgroup.id,
        title: itemgroup.title,
        visible: itemgroup.visible,
        listMode: itemgroup.listMode
      });

      itemgroup.layers.reverse().map(function (layer, key, ary) {
        switch (layer.maptype) {
          case "MapImageLayer":
            grouplayer.add(new MapImageLayer(layer.url, {
              id: layer.id,
              title: layer.title,
              visible: layer.visible,
              opacity: layer.opacity,
              listMode: layer.listMode
            }));
            break;
          case "PointCloudLayer":
            grouplayer.add(new PointCloudLayer(layer.url, {
              id: layer.id,
              title: layer.title,
              visible: layer.visible,
              // opacity: layer.opacity,
              listMode: layer.listMode
            }));
            break;
          case "TileLayer":
            let layervisible = false;
            if (window.xzcode == null || window.xzcode == "xuhui") {
              if (layer.id.indexOf("xuhui") != -1) {
                layervisible = true;
              } else {
                layervisible = false;
              }
            } else {
              if (layer.id.indexOf(window.xzcode) != -1) {
                layervisible = true;
              } else {
                layervisible = false;
              }
            }
            grouplayer.add(new TileLayer(layer.url, {
              id: layer.id,
              title: layer.title,
              visible: layervisible,
              opacity: layer.opacity,
              listMode: layer.listMode
            }));
            break;
          case "localFeatureLayer":
            grouplayer.add(new FeatureLayer({
              id: layer.id,
              title: layer.title,
              visible: layer.visible,
              opacity: layer.opacity,
              objectIdField: layer.objectIdField,
              geometryType: layer.geometryType,
              renderer: layer.renderer,
              listMode: layer.listMode,
              popupTemplate: layer.popupTemplate,
              fields: layer.fields
            }));
            break;
          case "FeatureLayer":
            grouplayer.add(new FeatureLayer(layer.url, {
              id: layer.id,
              title: layer.title,
              visible: layer.visible,
              opacity: layer.opacity,
              renderer: layer.renderer,
              listMode: layer.listMode,
              popupTemplate: layer.popupTemplate

            }));
            break;
          case "SceneLayer":
            grouplayer.add(new SceneLayer(layer.url, {
              id: layer.id,
              title: layer.title,
              visible: layer.visible,
              opacity: layer.opacity,
              listMode: layer.listMode,
              renderer: layer.renderer,
              definitionExpression: layer.definitionExpression,
              popupTemplate: layer.popupTemplate,
              popupEnabled: layer.popupEnabled,
              elevationInfo: layer.elevationInfo
            }));
            break;
          case "SHCTiledMapServiceLayer":
            var fullExtent = new Extent({
              "xmin": -65000,
              "ymin": -76000,
              "xmax": 75000.00000000003,
              "ymax": 72000.00000000003,
              "spatialReference": SpatialReference.WebMercator
            });
            grouplayer.add(SHCTiledMapServiceLayer({
              url: layer.url,
              fullExtent: fullExtent,
              opacity: layer.opacity,
              title: layer.title,
              id: layer.id,
              visible: layer.visible
            }));

            break;
          case "SHCMapServiceLayer":
            var fullExtent = new Extent({
              "xmin": -65000,
              "ymin": -76000,
              "xmax": 75000.00000000003,
              "ymax": 72000.00000000003,
              "spatialReference": SpatialReference.WebMercator
            });
            grouplayer.add(SHCMapServiceLayer({
              url: layer.url,
              fullExtent: fullExtent,
              opacity: layer.opacity,
              title: layer.title,
              id: layer.id,
              visible: layer.visible
            }));
            break;
        }
      })
      return grouplayer;
    })
  }

}
export default maplayersaddutils;
